<?php

namespace app\common\model;

use think\Cache;
use think\Db;
use think\Model;

/**
 * 采购入库资料数据模型
 */
class Procure extends Model
{

    public static function getList(){
        //$field = 'auto_id,warehousing_code,order_date,warehousing_date,factory_code,factory_name,row_id
        //inventory_number,quantity,warehousing_quantity,no_quantity,warehouse_id,shelves_id';
        /*$field = 'a.id,a.auto_id,a.warehousing_code,a.warehousing_date,a.factory_name,a.factory_code,
a.row_id,a.inventory_number,a.quantity,a.warehousing_quantity,a.no_quantity,
IFNULL(a.warehouse_id,0) warehouse_id,IFNULL((select name from fa_warehouse c where c.id=a.warehouse_id),"") warehouse_name,
IFNULL(a.shelves_id,0) shelves_id,IFNULL((select name from fa_shelves d where d.id=a.shelves_id),"") shelves_name,
b.name,IFNULL(b.specification,"") specification,b.attributes,b.unit_code,b.unit,IFNULL(a.remark,"") remark';*/
        $field = 'a.id,a.auto_id,a.warehousing_code,a.warehousing_date,a.factory_name,a.factory_code,
a.row_id,a.inventory_number,a.quantity,a.warehousing_quantity,a.no_quantity,
IFNULL(b.warehouse_id,0) warehouse_id,IFNULL(b.warehouse_name,"") warehouse_name,
IFNULL(a.shelves_id,0) shelves_id,IFNULL(b.shelves_name,"") shelves_name,
IFNULL(a.lastest_warehouse_id,0) lastest_warehouse_id,IFNULL(a.lastest_shelves_id,0) lastest_shelves_id,
IFNULL(a.lastest_warehouse_name,"") lastest_warehouse_name,IFNULL(a.lastest_shelves_name,"") lastest_shelves_name,
b.name,IFNULL(b.specification,"") specification,b.attributes,b.unit_code,b.unit,IFNULL(a.remark,"") remark';
        $data = self::field($field)->alias('a')
        ->join('inventory b','a.inventory_number=b.number','left')
        ->group('a.id')
        ->where([
            'a.no_quantity' => ['GT',0]
        ])
        ->order([
            'a.warehousing_code' => 'desc',
            'a.row_id' => 'asc'
        ])
        ->select();
        //$data = self::dealData($data);
        return $data;
    }

    /**
     * 按要求处理数据   poorder、poorder_item
     * @param $data
     * @return array
     */
    public static function dealData($data){
        $item  = $data;
        $poorder = array();
        $inventory = [];
        foreach ($data as $k=>$v){
            if(!isset($poorder[$v['warehousing_code']])){
                $poorder[$v['warehousing_code']] = [
                    'warehousing_code'=>$v['warehousing_code'],
                    'quantity' => $v['quantity'],
                    'warehousing_quantity' => $v['warehousing_quantity'],
                    'no_quantity'=>$v['no_quantity'],
                ];
            }else{
                $poorder[$v['warehousing_code']] = [
                    'warehousing_code'=>$v['warehousing_code'],
                    'quantity' => $poorder[$v['warehousing_code']]['quantity']+$v['quantity'],
                    'warehousing_quantity' => $poorder[$v['warehousing_code']]['warehousing_quantity']+$v['warehousing_quantity'],
                    'no_quantity'=>$poorder[$v['warehousing_code']]['no_quantity']+$v['no_quantity'],
                ];
            }
            $inventory[] =$v['inventory_number'];

        }
        //获取物料列表
        $inventoryField = 'id,number,name,specification,attributes';
        $inventoryList  = Db::name('inventory')->field($inventoryField)->where('id', 'in', $inventory)->select();
        foreach ($inventoryList as $kk=>$vv){
             $inventoryInfo[$vv['id']] = $vv;
        }
        foreach ($item as $k=>$v){
            if(isset($inventoryInfo[$v['inventory_number']])){
               $v['inventory_info'] = $inventoryInfo[$v['inventory_number']];
            }else{
                $v['inventory_info'] = null;
            }
        }
        return ['poorder'=>array_values($poorder),'item'=>$item];
    }



}
